var e = require("../utils.js");

function r(e) {
    return encodeURIComponent(e).replace(/%40/gi, "@").replace(/%3A/gi, ":").replace(/%24/g, "$").replace(/%2C/gi, ",").replace(/%20/g, "+").replace(/%5B/gi, "[").replace(/%5D/gi, "]");
}

exports.buildURL = function(i, n, t) {
    if (!n) return i;
    var a;
    if (t) a = t(n); else if (e.isURLSearchParams(n)) a = n.toString(); else {
        var c = [];
        e.forEach(n, function(i, n) {
            null != i && (e.isArray(i) ? n += "[]" : i = [ i ], e.forEach(i, function(i) {
                e.isDate(i) ? i = i.toISOString() : e.isObject(i) && (i = JSON.stringify(i)), c.push(r(n) + "=" + r(i));
            }));
        }), a = c.join("&");
    }
    if (a) {
        var s = i.indexOf("#");
        -1 !== s && (i = i.slice(0, s)), i += (-1 === i.indexOf("?") ? "?" : "&") + a;
    }
    return i;
};